package com.netrust.quarz.controller;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.netrust.quarz.dao.OriginalDao;
import com.netrust.quarz.dao.ServerDao;
import com.netrust.quarz.entity.Original;
import com.netrust.quarz.service.BulidAlarmHistoryService;
import com.netrust.quarz.service.BulidAlarmRealService;
import com.netrust.quarz.service.BulidStateService;
import com.netrust.quarz.utils.DateUtils;
import com.netrust.quarz.utils.ProdUtils;
import com.netrust.quarz.utils.http.ResultUtils;

@RestController
@RequestMapping("/q")
public class SyncController {
	@Autowired
	BulidAlarmHistoryService bulidAlarmHistoryService;
	@Autowired
	BulidAlarmRealService bulidAlarmRealService;
	@Autowired
	BulidStateService bulidStateService;
	@Autowired
	OriginalDao originalDao;
	@Autowired
	ServerDao serverDao;
	
	@GetMapping(value={"/doc"})
	public Map<String,Map<String,String>> docAdvice() throws Exception{
		Map<String,Map<String,String>> back=Maps.newLinkedHashMap();
		
		Map<String, String> mapAlarmHistory=Maps.newLinkedHashMap();
		mapAlarmHistory.put("op", "运营商的拼音简称：dxzwww|dxhlw|ydzwww|ydhlw");
		back.put("ah", mapAlarmHistory);
	
		Map<String, String> mapAlarmReal=Maps.newLinkedHashMap();
		mapAlarmReal.put("op", "运营商的拼音简称：dxzwww|dxhlw|ydzwww|ydhlw");
		back.put("ar", mapAlarmReal);
		
		Map<String, String> mapStatus=Maps.newLinkedHashMap();
		mapStatus.put("op", "运营商的拼音简称：dxzwww|dxhlw|ydzwww|ydhlw");
		back.put("st", mapStatus);
		
		Map<String, String> mapServer=Maps.newLinkedHashMap();
		mapStatus.put("op", "运营商的拼音简称：dxzwww|dxhlw|ydzwww|ydhlw");
		back.put("sr", mapServer);
		
		Map<String, String> mapOriginal=Maps.newLinkedHashMap();
		mapStatus.put("type", "原始数据类型：alarm_real|alarm_history|state|server|exception");
		mapStatus.put("op", "运营商的拼音简称：dxzwww|dxhlw|ydzwww|ydhlw");
		back.put("or", mapOriginal);
		
		return back;
	}
	
	@GetMapping("ah")
	public Object getAlarmHistory(@RequestParam("op") String op) throws Exception {
		
		List list=bulidAlarmHistoryService.http_run(op);
		
		return ResultUtils.success(list);
	}
	
	
	@GetMapping("ar")
	public Object getAlarmReal(@RequestParam("op") String op) throws Exception {
		
		List list=bulidAlarmHistoryService.http_run(op);
		
		return ResultUtils.success(list);
	}
	
	
	@GetMapping("st")
	public Object getState(@RequestParam("op") String op) throws Exception {
		
		String str=bulidStateService.http_run(op);
		
		return ResultUtils.success(str);
	}
	
	@GetMapping("sr")
	public Object getServers(@RequestParam("op") String op) throws Exception {
		
		List<String> list=bulidStateService.http_run_server(op);
		
		return ResultUtils.success(list);
	}
	
	@GetMapping("sr.obj")
	public Object getServerObjectIds(@RequestParam("op") String op) throws Exception {
		
		List<String> strs=bulidStateService.http_run_server(op);
		
		List<String> objectIds=Lists.newArrayList();
		for (String str : strs) {
			@SuppressWarnings("unchecked")
			Map<String,Object> map=JSONObject.parseObject(str, Map.class);
			objectIds.addAll(map.keySet());
		}
		
		return ResultUtils.success(objectIds);
	}
	
	@GetMapping("sr.flush")
	public Object flushServerObjectIds(@RequestParam("op") String op) throws Exception {
		
		List<String> strs=bulidStateService.http_run_server(op);
		
		List<String> objectIds=Lists.newArrayList();
		for (String str : strs) {
			@SuppressWarnings("unchecked")
			Map<String,Object> map=JSONObject.parseObject(str, Map.class);
			objectIds.addAll(map.keySet());
		}
		
		return ResultUtils.success(objectIds);
	}
	
	@GetMapping("or")
	public Object original(@RequestParam("type") String type,@RequestParam("op") String op) throws Exception {
		
		List<Original> list=originalDao.findByNameLike("%"+type+"||"+ProdUtils.op2Ip(op)+"%");
		
		return ResultUtils.success(list);
	}
	
	@GetMapping("or.obj")
	public Object originalObj(@RequestParam("type") String type,@RequestParam("op") String op,@RequestParam("start") String start) throws Exception {
		
		if(StringUtils.isBlank(start)){
			start=DateUtils.formatDate(DateUtils.addMins(new Date(),-90),"yyyyMMddHHmmss");
		}
		
		Date startDate=DateUtils.parseDate(start, "yyyyMMddHHmmss");
		
		Date endDate=DateUtils.addMins(startDate, 30);
		
		List<Original> list=originalDao.findByNameLikeAndTimeBetween("%"+type+"||"+ProdUtils.op2Ip(op)+"%",startDate,endDate);
		
		List<String> strs=(List<String>) CollectionUtils.collect(list, new Transformer() {
			@Override
			public Object transform(Object o) {
				return ((Original)o).getOriginalJson();
			}
		});
		
		List<String> objectIds=Lists.newArrayList();
		
		for (String str : strs) {
			Map<String,Object> map=JSONObject.parseObject(str, Map.class);
			objectIds.addAll(map.keySet());
		}
		
		return ResultUtils.success(objectIds);
	}
	
	
	
	@SuppressWarnings("unchecked")
	@GetMapping("or.obj.check")
	public Object check(@RequestParam("type") String type,@RequestParam("op") String op,@RequestParam("start") String start) throws Exception {
		
		if(StringUtils.isBlank(start)){
			start=DateUtils.formatDate(DateUtils.addMins(new Date(),-90),"yyyyMMddHHmmss");
		}
		
		Date startDate=DateUtils.parseDate(start, "yyyyMMddHHmmss");
		
		Date endDate=DateUtils.addMins(startDate, 30);
		
		List<Original> list=originalDao.findByNameLikeAndTimeBetween("%"+type+"||"+ProdUtils.op2Ip(op)+"%",startDate,endDate);
		
		List<String> strs=(List<String>) CollectionUtils.collect(list, new Transformer() {
			@Override
			public Object transform(Object o) {
				return ((Original)o).getOriginalJson();
			}
		});
		
		List<String> objectIds=Lists.newArrayList();
		
		for (String str : strs) {
			Map<String,Object> map=JSONObject.parseObject(str, Map.class);
			objectIds.addAll(map.keySet());
		}
		
		List<Object> servers=serverDao.findServerObjectId(ProdUtils.op2oe(op).getName());
		
		List<String> serversList=(List<String>) CollectionUtils.collect(servers, new Transformer() {
			@Override
			public Object transform(Object o) {
				return String.valueOf(o);
			}
		});
		
		Collection<String> checkList=CollectionUtils.intersection(objectIds, serversList);
		
		return ResultUtils.success(checkList);
	}
}
